package org.peterf.foxish.component;

import org.peterf.foxish.camera.Camera;
import org.peterf.foxish.component.property.Updatable;
import org.peterf.foxish.component.updater.interfaces.Updater;
import org.peterf.foxish.math.transformation.Matrix2D;
import org.peterf.foxish.math.transformation.SpriteMatrix;

/**
 * A {@link Sprite} is a basic {@link Geometry} whose position, rotation angle,
 * skew angle, scale and color can be modified.
 * 
 * @author peter
 * 
 */
public abstract class Sprite extends Geometry implements Updatable {

    protected float[] color = new float[] { 1.0f, 1.0f, 1.0f, 1.0f };

    protected Updater updater = null;
    
    protected boolean enabled = true;
    
    protected SpriteMatrix matrix = new SpriteMatrix().setIdentity();

    public float getAlpha() {
        return color[3];
    }

    public float getB() {
        return color[2];
    }

    public float getG() {
        return color[1];
    }

    public float getR() {
        return color[0];
    }

    public Updater getUpdater() {
        return updater;
    }
    
    public void setEnable(boolean enabled) {
        this.enabled = enabled;
    }

    public void removeUpdater() {
        this.updater = null;
    }

    public synchronized void setAlpha(float alpha) {
        color[3] = alpha;
    }

    public synchronized void setColor(float r, float g, float b) {
        color[0] = r;
        color[1] = g;
        color[2] = b;
    }

    public void setUpdater(Updater updater) {
        this.updater = updater;
    }

    protected void onUpdate(float elapsed) {
        if (this.updater != null) {
            if (updater.isFinished()) {
                updater = null;
            } else {
                updater.update(this, elapsed);
            }
        }
    }

    public SpriteMatrix getMatrix() {
        return matrix;
    }

}
